#pragma once

#include "my_cache_err.h"
#include "my_cache_key.h"
#include "comm/my_buf.h"
#include "event/my_event.h"

typedef struct my_cache_req_s my_cache_req_t;

typedef void(*my_cache_req_handler_pt)(my_cache_req_t *req);

struct my_cache_req_s {
    // request params
    my_cache_key_t              *key;
    my_buf_chain_t              *data;
    my_bool_t                   is_write;

    // context
    void                        *ctx_data;
    my_cache_req_handler_pt     handler;

    // schedule
    my_event_t                  event;
    my_dispatch_event_t         dispatch_event;

    // response
    my_cache_err_t              err;
    const char                  *errmsg;
};

static inline void
my_cache_req_init(my_cache_req_t *req, my_cache_key_t *key,
    my_buf_chain_t *data, void *ctx_data, my_cache_req_handler_pt handler) {

    bzero(req, sizeof(my_cache_req_t));

    req->key = key;
    req->data = data;
    req->ctx_data = ctx_data;
    req->handler = handler;

    // no need to do this anymore
    // my_event_init(&req->event);
    // my_dispatch_event_init(&req->dispatch_event);

    req->err = CACHE_ERR_NO_ERR;
}
